<?php
/*
 * 存放相关的配置, 一些用到的扩展函数是放在这个里边的
 * @package    auto
 * @author     Mark
 * @copyright  Copyright (c) 2013 ixdcw team
 * @version    $Id: 7b71050b7f8465417b59cbf2e8efd70280a15937 $
 */
defined('IN_IXDCW') or exit('Access Denied');
/**
 * 类的分离和大规模改写
 * Class AutoCategory
 */
class AutoCategory {

	// 缓存的类别
	const TYPE_RANK        = 'rank';
	const TYPE_ENGINE      = 'engine';
	const TYPE_LEVEL       = 'level';
	const TYPE_EMISSION    = 'emission';
	const TYPE_GEARBOX     = 'gearbox';
	const TYPE_COUNTRY     = 'country';
	const TYPE_YEAR        = 'year';
	const TYPE_PICTURE     = 'picture';
	const TYPE_PARAM_GROUP = 'param_group';
	const TYPE_HANG        = 'hang';

	const TYPE_RANK_NEWENERGY = 432;

	// 级别的绑定
	const LEVEL_PASSANGER  = 433;
	const LEVEL_COMMERCIAL = 434;
	const LEVEL_RV         = 435;

	const SUGGEST_HOT = 'hot';
	const SUGGEST_NEW = 'new';

	/**
	 * 级别分表中允许的类型
	 * @var array
	 */
	private static $_types = array(
		self::TYPE_RANK        => '品牌/生产商/车系',
		self::TYPE_ENGINE      => '动力类型',
		self::TYPE_LEVEL       => '级别',
		self::TYPE_EMISSION    => '排放标准',
		self::TYPE_GEARBOX     => '变速箱',
		self::TYPE_COUNTRY     => '国别',
		self::TYPE_YEAR        => '年款',
		self::TYPE_PICTURE     => '图片分类',
		self::TYPE_PARAM_GROUP => '车款参数分组',
		self::TYPE_HANG        => '悬挂类型',
	);

	/**
	 * 获取所有的缓存类型
	 * @return array
	 */
	public static function getTypes() {
		return self::$_types;
	}

	/**
	 * 后台生成的easyui的树
	 * @param int $parentid
	 * @return array
	 */
	public static function easyuiTree($parentid = 0) {
		global $db;
		$table = Table::name(Table::NAME_AUTO_CATEGORY);
		$sql   = "select arid, name as text, letter, parentid, level from {$table} where typename='rank' and parentid=$parentid order by letter, arid desc";
		$items = $db->getAll($sql);
		foreach ($items as $k => $v) {
			if ($v['parentid'] == '0') {
				$items[$k]['text'] = $v['letter'] . '-' . $v['text'];
			}
			if ($v['level'] == '0') {
				$items[$k]['attributes'] = array('ltype' => 'brand', 'id' => $v['arid'],);
				$items[$k]['state']      = 'closed';
			} else if ($v['level'] == '1') {
				$items[$k]['attributes'] = array('ltype' => 'manufactor', 'id' => $v['arid'],);
				$items[$k]['state']      = 'closed';
			} else if ($v['level'] == '2') {
				$items[$k]['attributes'] = array('ltype' => 'car', 'id' => $v['arid'],);
			}
		}
		return Ext::genTree($items);
	}

	/**
	 * 获取缓存
	 * @param $type
	 * @return array|mixed|string
	 */
	public static function getCache($type) {
		static $_cache = array();
		if (!isset($_cache[$type])) {
			$_cache[$type] = PCache::read('autoCategory-' . $type . '.php');
		}
		return $_cache[$type];
	}

	/**
	 * 获取缓存的条目
	 * @param        $type
	 * @param        $itemid
	 * @param string $key
	 * @return mixed
	 */
	public static function getCacheItem($type, $itemid, $key = 'name') {
		$cache = self::getCache($type);
		if (empty($key)) {
			return $cache[$itemid];
		} else {
			if ($itemid) {
				return $cache[$itemid][$key];
			}
			return '';
		}
	}

	/**
	 * 获取类型的说明
	 * @param $typename
	 * @return mixed
	 */
	public static function typeDesc($typename) {
		return self::$_types[$typename];
	}

	/**
	 * 推荐车系
	 * @param        $arid
	 * @param        $num
	 * @return array
     * @param string $type
	 */
	public static function suggest($arid, $num, $type = self::SUGGEST_HOT) {
		global $db;
		$LEVEL         = self::getCache(self::TYPE_LEVEL);
		$tableCategory = Table::name(Table::NAME_AUTO_CATEGORY);
		$tableRankCar  = Table::name(Table::NAME_AUTO_RANK_CAR);
		$arrChildids   = $LEVEL[$arid]['arrchildid'];
		$order         = '';
		if ($type == self::SUGGEST_HOT) {
			$order = 'order by c.edittime DESC';
		} else if ($type == self::SUGGEST_NEW) {
			$order = 'order by c.addtime DESC';
		}
		$sql = "select c.arid, c.linkurl, c.name, rc.car_thumb as thumb FROM $tableCategory c,$tableRankCar rc where c.arid=rc.arid and rc.suggest = 1 and rc.level in ($arrChildids) $order limit $num";
		return $db->getAll($sql);
	}

	public static function getLocation($arid, $CACHE, $name = '不限') {
		$lpids   = $CACHE[$arid]['arrparentid'] . ',' . $arid;
		$arrPids = explode(',', $lpids);
		array_shift($arrPids);
		$arrLocation   = array();
		$arrLocation[] = array(
			'arid' => 0,
			'name' => $name,
		);
		foreach ($arrPids as $v) {
			$arrLocation[$v] = $CACHE[$v];
		}
		return $arrLocation;
	}

	public static function catParents($type, $catid) {
		if (!$catid) {
			return array(0);
		}
		$cache = self::getCache($type);
		$strPids = $cache[$catid]['arrparentid'];
		$arrPids = explode(',', $strPids);
		$arrPids[] = $catid;
		return $arrPids;
	}


	public static function catChildren($type, $parentid) {
		$cache = self::getCache($type);
		$cats = array();
		foreach($cache as $k => $v) {
			if ($v['parentid'] == $parentid) {
				$cats[$k] = $v;
			}
		}
		return $cats;
	}
}